package com.jz.netthings.bloodsources.controller;


import com.jz.netthings.bean.system.JyRole;
import com.jz.netthings.bloodsources.bean.XyLink;
import com.jz.netthings.bloodsources.bean.XyNoPulping;
import com.jz.netthings.bloodsources.bean.XyRemind;
import com.jz.netthings.bloodsources.constant.BloodSourcesConstant;
import com.jz.netthings.bloodsources.service.XyLinkService;
import com.jz.netthings.bloodsources.service.XyNoPulpingService;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.security.utils.Token;
import com.jz.netthings.support.SimpleCoreResponse;
import com.jz.netthings.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 未献浆记录表 前端控制器
 * </p>
 *
 * @author 张凡
 * @since 2019-08-13
 */
@RestController
@RequestMapping("/adscm/bloodSources/xyNoPulping")
public class XyNoPulpingController {
    @Autowired
    XyLinkService xyLinkService;
    @Autowired
    XyNoPulpingService xyNoPulpingService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @RequestMapping("/queryByCondition")
    public SimpleCoreResponse queryByCondition(
            HttpServletRequest request,
            String nurseName,
            String providerName,
            String createTimeLess,
            String createTimeGreater,
            String currentPage,
            String pageSize
    ) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<JyRole> jyRoles = token.getUser().getRoles();
        String useNurseDeptId = null;
        Integer useNurseId = null;
        if(jyRoles!=null && jyRoles.size()>=1){
            //证明该登录用户有角色信息，则进行其拥有角色遍历查找，查看其是否拥有相关科室主任角色
            Boolean isDirector = false;
            Boolean isMaster = false;
            for (JyRole jyRole : jyRoles) {
                if(BloodSourcesConstant.ROLES_MASTER_CODE.equals(jyRole.getRole_code())){     //如果是站长则直接条件查询
                    isMaster = true;
                    break;
                }
            }
            for (JyRole jyRole : jyRoles) {
                if(BloodSourcesConstant.ROLES_DIRECTOR_CODE.equals(jyRole.getRole_code())){  //如果包含科室主任角色
                    isDirector = true;
                    break;
                }
            }
            if(isMaster){
                //如果是站长，就查询所有，查询约束值 useNurseDeptId，useNurseId 则不用赋值
            }else{
                // a.如果当前登录用户拥有   【科室主任角色】 ==》则获取其所属科室ID==》通过其科室ID（userDeptId = token.getUser().getDeptId();） 获取数据表同属其科室（同deptId）的所有血源链接护士的数据，并可以修改
                if(isDirector){
                    Integer userDeptId = token.getUser().getDeptId();  //用户部门Id
                    useNurseDeptId = userDeptId+"";
                }else{
                    // b.如果当前登录用户未拥有 【科室主任角色】 ==》则获取其用户ID(token.getUser().getId())==>>通过其护士Id(useNurseId《==》userId) 进行数据条件拉取
                    Integer userId = token.getUser().getId();
                    useNurseId = userId;
                }
            }
        }else {

        }
        return new SimpleCoreResponse("200","查询成功", xyNoPulpingService.queryByCondition(useNurseId,nurseName,useNurseDeptId,providerName,createTimeLess,createTimeGreater,currentPage,pageSize));
    }
    @RequestMapping("/updateXyNoPulpingInfo")
    public SimpleCoreResponse updateXyNoPulpingInfo(@RequestBody XyNoPulping XyNoPulping, HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        //获取当前未献浆记录的浆员ID,获取其是否继续提醒的判定值
        Integer providerId = XyNoPulping.getProviderId();                 //获取浆员的ID
        Integer isPush = XyNoPulping.getIsPushMsg(); //1.true 2.false
        if(providerId!=null && providerId!=0){
            //同步状态到血源链接表的【是否献浆提醒】判定值，该值为枚举值，是or否 且使用中文赋值.
            XyLink xyLink =  new XyLink();
            xyLink.setLinkProviderId(Long.parseLong(providerId+""));
            if(isPush==1){
                xyLink.setIsPulpingMsg("是");
            }else {
                if(isPush==2){
                    xyLink.setIsPulpingMsg("否");
                }
            }
            xyLinkService.updateByProviderId(xyLink);
        }
        Boolean result= xyNoPulpingService.updateByNoPulpingInfo(XyNoPulping);
        return new SimpleCoreResponse("200","查询成功" ,result);
    }
}

